<template>
    <div class="app-container">
      <bpmn v-if="show" :bpmnInfo="info" :formVersionId="formVersionId"  @save="onSaveProcess"></bpmn>
    </div>
</template>
<script>
import bpmn from "@/views/tool/bpmn/index"
import { getAppVersion, updateAppVersion, addBpmnConfig } from "@/api/flowable/appVersion";

export default{
  name:"Designer",
  components:{bpmn},
  data () {
    return {
      info:{
        xml:"",
        processId:'',
        processName: this.$route.query.appName
      },
      show:false,
      appVersionId: this.$route.query.appVersionId,
      formVersionId: this.$route.query.formVersionId,
      deployId: '',
    }
  },
  created() {
    this.getAppVersion();
  },
  methods:{
    onSaveProcess(xml){
      this.toSaveBpmnData();
      if(this.deployId){
        this.$modal.confirm('该流程已经部署过了。流程图修改不会被保存').then(()=>{
          this.closeSelf();
        })
        return;
      }
      //todo 判断有deployId说明部署过了。不能修改xml了。只能修改表单权限
      this.$modal.confirm('确认保存流程图？').then(res=> {
        // console.log(xml)
        updateAppVersion({versionId:this.appVersionId, bpmnXml:xml}).then(res =>{
          this.$modal.msgSuccess(res.msg)
          this.closeSelf();
        })
      })
    },
    getAppVersion() {
      getAppVersion(this.appVersionId).then(res=>{
        this.info.xml = res.data.bpmnXml;
        this.deployId = res.data.deployId;
        if (this.deployId) {
          this.$modal.confirm('该流程已经部署过了。流程图修改不会被保存，只能修改表单权限')
        }
        this.show = true;
      })
    },
    closeSelf(){
      this.$store.dispatch("tagsView/delView", this.$route);
      this.$router.go(-1)
    },
    toSaveBpmnData() {
      // console.log('toSaveBpmnData')

      if(JSON.stringify(this.$store.state.bpmnNodeSettings.nodeConfig) === '{}' && this.$store.state.bpmnNodeSettings.rejectNode === '') {
          return;
      }
      // console.log(this.$store.state.bpmnNodeSettings.nodeConfig)
      // console.log(this.$store.state.bpmnNodeSettings.rejectNode)

      let params = {appVersionId: this.appVersionId}
      if(JSON.stringify(this.$store.state.bpmnNodeSettings.nodeConfig) !== '{}') {
          Object.assign(params,{
            nodeConfig: JSON.stringify(this.$store.state.bpmnNodeSettings.nodeConfig)
          })
      }

      if(this.$store.state.bpmnNodeSettings.rejectNode !== '') {
          Object.assign(params,{
            rejectNode: this.$store.state.bpmnNodeSettings.rejectNode
          })
      }

      addBpmnConfig(params).then(res=>{
        this.$store.commit('bpmnNodeSettings/UPDATE_CONFIGNODE', {})
        this.$store.commit('bpmnNodeSettings/UPDATE_REJECTNODE', '')
      })
    },

  }
}
</script>
<style>
</style>
